package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.ChanPinXiangQingDaoImpl;
import com.ysd.entity.ChanPinXiangQing;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminProductServlet extends BaseServlet {

	/**
	 * 多条件查询商品 localhost:8080/qiuxieshangcheng/adminProduct?reqName=getProducts
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProducts(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 接受前台传来的参数
		String chanPinId = request.getParameter("chanPinId");
		String chanPinName = request.getParameter("chanPinName");
		String chanPinGongNeng = request.getParameter("chanPinGongNeng");
		String chanPinShouJia = request.getParameter("chanPinShouJia");
		String shiChangJia = request.getParameter("shiChangJia");
		String shangPinPinPai = request.getParameter("shangPinPinPai");
		String chanPinXiangQingLoGo = request.getParameter("chanPinXiangQingLoGo");
		int beginProductPrice = 0;
		if (request.getParameter("beginProductPrice") != null
				&& !request.getParameter("beginProductPrice").equals("")) {
			beginProductPrice = Integer.parseInt(request.getParameter("beginProductPrice"));
		}

		int endProductPrice = 0;
		if (request.getParameter("endProductPrice") != null && !request.getParameter("endProductPrice").equals("")) {
			endProductPrice = Integer.parseInt(request.getParameter("endProductPrice"));
		}

		int xiangQingWaiJianId = 0;
		if (request.getParameter("xiangQingWaiJianId") != null
				&& !request.getParameter("xiangQingWaiJianId").equals("")) {
			xiangQingWaiJianId = Integer.parseInt(request.getParameter("xiangQingWaiJianId"));
		}

		ChanPinXiangQing cpxq = new ChanPinXiangQing(chanPinId, chanPinName, chanPinGongNeng, chanPinShouJia,
				shiChangJia, shangPinPinPai, chanPinXiangQingLoGo, beginProductPrice, endProductPrice,
				xiangQingWaiJianId);

		ChanPinXiangQingDaoImpl cpDao = new ChanPinXiangQingDaoImpl();
		List<ChanPinXiangQing> pList = cpDao.getProductsByWhere(cpxq);

		Gson gson = new Gson();
		String res = gson.toJson(pList);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		out.print(res);
		out.flush();
		out.close();
	}

	/**
	 * 添加产品 localhost:8080/qiuxieshangcheng/adminProduct?reqName=deleteProducts
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addProducts(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out = response.getWriter();//回信者
		ChanPinXiangQingDaoImpl pcDao = new ChanPinXiangQingDaoImpl();// 产品实现类对象
		// 接受前台传来的参数
		String names = request.getParameter("chanPinName");
		String chanPinGongNeng = request.getParameter("chanPinGongNeng");
		String chanPinShouJia = request.getParameter("chanPinShouJia");
		String shangPinPinPai = request.getParameter("shangPinPinPai");
		String chanPinXiangQingLoGo = request.getParameter("chanPinXiangQingLoGo");
		int xiangQingWaiJianId = Integer.parseInt(request.getParameter("xiangQingWaiJianId"));

		if (names != null && !names.equals("")) {
			List<ChanPinXiangQing> xqList = pcDao.getChanPins(names);
			if (xqList.size() == 0) {
				ChanPinXiangQing cpxq1 = new ChanPinXiangQing("", names, chanPinGongNeng, chanPinShouJia, "", shangPinPinPai, chanPinXiangQingLoGo, 0, 0, xiangQingWaiJianId);

				int n = pcDao.addProduct(cpxq1);

				if (n > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "产品已存在", ""));
			}
		}

		out.flush();
		out.close();
	}

	// 删除

	public void deleteProducts(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int cId = Integer.parseInt(request.getParameter("categoryId"));
		ChanPinXiangQingDaoImpl pcDao = new ChanPinXiangQingDaoImpl();
		int n = pcDao.deleteProductById(cId);

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		if (n > 0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		out.flush();
		out.close();

	}
	/**
	 * 添加产品 localhost:8080/qiuxieshangcheng/adminProduct?reqName=getProById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out = response.getWriter();//回信者
		ChanPinXiangQingDaoImpl pcDao = new ChanPinXiangQingDaoImpl();// 产品实现类对象
		// 接受前台传来的参数
		
		int Id = Integer.parseInt(request.getParameter("cId"));
		ChanPinXiangQing cp = pcDao.getCPById(Id);
		
		Gson gson = new Gson();
		String res = gson.toJson(cp);
		
		out.print(res);
		
		out.flush();
		out.close();
		
		
		
	}
	
	/**
	 * 添加产品 localhost:8080/qiuxieshangcheng/adminProduct?reqName=updatePro
	 * $("#cpId").val(res.chanPinId);
            		$("#cpName").val(res.chanPinName);
            		$("#cpGN").val(res.chanPinGongNeng);
            		$("#cpJG").val(res.chanPinShouJia);
            		$("#cpPP").val(res.shangPinPinPai);
            		$("#cpTP").val(res.chanPinXiangQingLoGo);
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updatePro(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out = response.getWriter();//回信者
		ChanPinXiangQingDaoImpl pcDao = new ChanPinXiangQingDaoImpl();// 产品实现类对象
		// 接受前台传来的参数
		String chanPinId = request.getParameter("cpId");
		String chanPinName = request.getParameter("cpName");
		String chanPinGongNeng = request.getParameter("cpGN");
		String chanPinShouJia = request.getParameter("cpJG");
		String shangPinPinPai = request.getParameter("cpPP");
		String chanPinXiangQingLoGo = request.getParameter("cpTP");
		
		List<ChanPinXiangQing>   pcList=   pcDao.getChanPins(chanPinName);
		if (pcList.size()==0) {
			ChanPinXiangQing cp = new ChanPinXiangQing(chanPinId, chanPinName, chanPinGongNeng, chanPinShouJia, shangPinPinPai, chanPinXiangQingLoGo);
			int n = pcDao.updateCP(cp);
			if (n>0) {
				out.print(Result.toClient(true, "修改成功",""));
			} else {
				out.print(Result.toClient(false, "修改失败",""));
			}
		} else {
			out.print(Result.toClient(false, "xinWenFebLieName",""));
		}
		
		
		
		
		out.flush();
		out.close();
		
		
		
	}
	
	
	
	
	
	
	
	
	
}
